package interceptor;
import java.io.Reader;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class IbatisInterceptor implements Interceptor{
	private static Reader reader;
	private static SqlMapClient sqlMapper;
	public void destroy() {
		reader = null;
		sqlMapper = null;
	}
	public void init() {
		try{
			reader = Resources.getResourceAsReader("sqlMapConfig.xml");
			sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
			reader.close();
		}catch(Exception e){e.printStackTrace();}
	}
	
	public String intercept(ActionInvocation action) throws Exception {
		Object obj = action.getAction();
		if(obj instanceof IbatisAware){
			IbatisAware ibatis = (IbatisAware)obj;
			ibatis.setIbatis(sqlMapper);
		}
		return action.invoke();
	}
	
}














